package hostsystem;

import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ConfirmBooking extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		HttpSession session = req.getSession();
		String[] staffcheck = req.getParameterValues("staffcheck");
		String date = req.getParameter("date");
		String time = req.getParameter("time");

		if (session.getAttribute("islogin") == null) {
			resp.sendRedirect("login.html");
			return;
		}

		String bookingid = "";

		List<Staffprofile> staffs = new ArrayList<Staffprofile>();
		String bookingtime = "";

		if (staffcheck != null && !staffcheck.equals("")) {
			Connection con = hostdb.getConnection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			staffs = (List<Staffprofile>) session.getAttribute("staffs");
			int workhour = 0;
			String staff = "";
			Double totwage = 0.0;

			try {
				DateFormat dateFormat = new SimpleDateFormat("yyMMdd");
				Date cdate = new Date();
				String curdate = dateFormat.format(cdate).toString();
				ps = con.prepareStatement("select max(substr(booking_id,7)) from job_booking where substr(booking_id,1,6)=?");
				ps.setString(1, curdate);
				rs = ps.executeQuery();
				int count = 0;
				rs.next();
				if (rs.getString(1) != null) {
					count = Integer.parseInt(rs.getString(1));
				}
				bookingid = curdate + String.format("%04d", count + 1);
				dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				bookingtime = dateFormat.format(cdate).toString();

			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			if (time.equals("3")) {
				workhour = 6;
			}
			if (time.equals("1") || time.equals("2")) {
				workhour = 3;
			}
			// insert to table
			for (int i = 0; i < staffcheck.length; i++) {
				Staffprofile a = (Staffprofile) staffs.get(Integer
						.parseInt(staffcheck[i]) - 1);
				staff = staff + a.getName() + "<BR>";
				totwage = totwage + a.getWage();
				try {

					ps = con.prepareStatement("insert into time_table  (staff_id,booking_id,date,time) values(?,?,?,?)");
					ps.setInt(1, a.getStaffid());
					ps.setString(2, bookingid);
					ps.setString(3, date);
					ps.setString(4, time);
					ps.executeUpdate();

					ps = con.prepareStatement("insert into job_booking (booking_id,staff_id,work_hour,account_id,job_status,bookingtime) values(?,?,?,?,1,?)");
					ps.setString(1, bookingid);
					ps.setInt(2, a.getStaffid());

					ps.setInt(3, workhour);
					ps.setString(4, (String) session.getAttribute("accountid"));
					ps.setString(5, bookingtime);
					ps.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				con.commit();
				ps.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			req.setAttribute("bookingid", bookingid);
			req.setAttribute("date", date);
			req.setAttribute("workhour", workhour);
			req.setAttribute("staff", staff);
			req.setAttribute("totwage", totwage);
			req.setAttribute("totpay", totwage * workhour);
			req.setAttribute("msg", "Booking created!");
			req.getRequestDispatcher("/bookingsummary.jsp").forward(req, resp);
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.sendRedirect("login.html");
	}
}